<?php

/**
 * This is the model class for table "zy_user".
 *
 * The followings are the available columns in table 'zy_user':
 * @property integer $user_id
 * @property string $user_name
 * @property string $password
 * @property integer $parentId
 * @property integer $level
 */
class User extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return User the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'zy_user';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('user_name,level,password,telephone,email,first_name,last_name', 'required'),
			array('user_name','unique'),
			array('parentId, level', 'numerical', 'integerOnly'=>true),
			array('user_name, password,first_name,last_name', 'length', 'max'=>255),
			array('invite_code', 'length', 'max'=>200),
			array(' password', 'length', 'min'=>6),
		//	array(' telephone', 'length', 'min'=>11),
			array('email','email'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('user_id, user_name, password, parentId, level,first_name,last_name,invite_code,iscustomer', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
				'userservice'=>array(self::HAS_MANY, 'UserService', 'user_id'),
		        'partner'=>array(self::BELONGS_TO,'PartnerInfo','','on'=>'t.user_name=partner.company_name'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'user_id' => 'User_id',
			'user_name' => Yii::t('base','UserName'),
			'first_name'=>Yii::t('base','FirstName'),
			'last_name'=>Yii::t('base','LastName'),
			'invite_code'=>Yii::t('base','Invite_code'),
			'password' => Yii::t('base','Password'),
			'parentId' => Yii::t('base','ParentId'),
			'level' => Yii::t('base','Level'),
			'email'=>Yii::t('base','email'),
			'telephone'=>Yii::t('base','telephone'),
		    'iscustomer' => Yii::t('base','Iscustomer'),
		    'invite_user_id'=>Yii::t('base','Invite User ID')
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search($results)
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.
	//	echo $this->level.">>>";die();
		$criteria=new CDbCriteria;
		$criteria->compare('user_id',$this->user_id);
		$criteria->addInCondition('user_id',$results);
		$criteria->compare('user_name',$this->user_name,true);
		$criteria->compare('invite_code',$this->invite_code,true);
		$criteria->compare('telephone',$this->telephone,true);
		$criteria->compare('email',$this->email,true);
		$criteria->compare('parentId',$this->parentId);
		$criteria->compare('level',$this->level);
		$criteria->compare('iscustomer',$this->iscustomer);
		$criteria->compare('invite_user_id',$this->invite_user_id);
		$criteria->with='partner';
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
		public function search2()
	{
		
		$criteria=new CDbCriteria;
		$criteria->compare('user_id',$this->user_id,false,"or");
		$criteria->compare('parentId',$this->parentId,false,"or");
		$criteria->compare('user_name',$this->user_name,true,"and");
		$criteria->compare('level',$this->level,true,"and");
		$criteria->compare('telephone',$this->telephone);
		$criteria->compare('email',$this->email);
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'sort'=>array(

					'defaultOrder'=>'parentId ASC',

			),
			'pagination'=>array(

					'pageSize'=>15,

			),
		));
	}
	public function validatePassword($password)

	{

		return $this->encrypt($password)===$this->password;

	}

	

	/**

	 * return md5 string of the input one.

	 * @param string $pass

	 */

	public function encrypt($pass)

	{

		return $pass;

	}
	public function getUserName($id)

	{

		$user = User::model()->findByPk($id);

		return $user->user_name;

	}
	public function getAllTopUser($user_id)

	{
		$id=Yii::app()->user->id;

		if($user_id!=""){

			$returnArr = User::model()->findAll('user_id='.$user_id);

			return CHtml::listData($returnArr,'user_id','user_name');

		}else{

		$returnArr = User::model()->findAll('parentId='.$id);

		return CHtml::listData($returnArr,'user_id','user_name');
		}

	}
	public function getUserId($name)
	{
		if($name!=""){
		$sql = "select user_id from zy_user where user_name ='".$name."'";
		$connection = Yii::app()->db;

		$command = $connection->createCommand($sql);

		$results = $command->queryAll();

		for($i=0;$i<count($results);$i++){

			$result[$i]=$results[$i]['user_id'];

		}
		return $result;
		}
	}
	public function getLevelName($id)
	{
		$rs = User::model()->findBypk($id);
		if($rs->level==1){
			$rs->level=Yii::t('base','Administrator');
		}elseif($rs->level==2){
			$rs->level=Yii::t('base','Master Account');
		}elseif($rs->level==3){
			$rs->level=Yii::t('base','Sub Account');
		}elseif($rs->level==4){
			$rs->level=Yii::t('base','Company');
		}elseif($rs->level==5){
			$rs->level=Yii::t('base','Staff');
		}elseif($rs->level==6){
			$rs->level=Yii::t('base','Salesman');
		}elseif($rs->level==7){
			$rs->level=Yii::t('base','Person');
		}elseif($rs->level == 9){
			$rs->level=Yii::t('base','Partners');
		}elseif($rs->level == 10){
			$rs->level=Yii::t('base','Awards');
		}elseif($rs->level == 11){
			$rs->level=Yii::t('base','Sales manager');
		}elseif ($rs->level == 12)
		{
		$rs->level=Yii::t('base','Telesales People');
		}
		return $rs->level;
	}
	
	//得到电话销售人员
	public function getSalespeople()
	{
			$returnArr = User::model()->findAll('level=12');
			//return CHtml::listData($returnArr,'user_name','user_name');
			return $returnArr;
	}
	//判断用户是否有设备在手上
	public function ishaveEquipment($id){
		$sql = "select * from zy_user_service a left join zy_equipment b on a.equipment_id = b.equipment_id where b.is_used = 1 AND a.access_id=".$id;
		$connection = Yii::app()->db;
		$command = $connection->createCommand($sql);

		$results = $command->queryAll();

		for($i=0;$i<count($results);$i++){

			$result[$i]=$results[$i]['user_service_id'];

		}

		return $result;
	}
	public function isagenthaveEquipment($id){
		$result=Equipment::model()->find('updater='.$id.' AND is_used=1');
		return $result;
		
	}
	//根据id判断用户的level
	public function getlevel($id){
		$result=User::model()->find('user_id='.$id);
		return $result->level;
	}
	
	//验证用户是否购买智能路由服务
	public function ckAccount(){
		$sql="select * from zy_user_service where user_id=".yii::app()->user->id." and service_id = 8";
		$connection = Yii::app()->db;
		$command = $connection->createCommand($sql);
		$results = $command->queryAll();
		if(!empty($results)){
			return true;
		}else{
			return false;
		}
		
	}
	
	//显示是否是真实用户 int型，0：测试账号，1:真实用户
	public function getCustomer($iscustomer)
	{
		if($iscustomer==1)
		      return Yii::t('base','Customer Account');
		else if($iscustomer==0)
		      return Yii::t('base','Test Account');
	    
	    
	}
}